package com.zmj.modules.flow.service;

import com.zmj.framework.common.CommonConstant;
import com.zmj.framework.exception.remote.RemoteException;
import com.zmj.framework.response.Result;
import com.zmj.modules.flow.client.FlowableClient;
import com.zmj.modules.flow.entity.dto.BpmProcessStartQueryDto;
import com.zmj.modules.flow.entity.dto.BpmTaskAssigneeDTO;
import com.zmj.modules.flow.entity.dto.BpmTaskBackQueryDTO;
import com.zmj.modules.flow.entity.dto.BpmTaskCcQueryDto;
import com.zmj.modules.flow.entity.dto.BpmTaskFinishQuery;
import com.zmj.modules.flow.entity.dto.BpmTaskQueryDto;
import com.zmj.modules.flow.entity.vo.ProcessInstanceVO;
import com.zmj.modules.flow.entity.vo.TaskAssigneeVO;
import com.zmj.modules.flow.entity.vo.TaskBackVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lzy
 * @since 2023-12-01
 */
@Service
@Slf4j
public class FlowableService {
    @Resource
    private FlowableClient flowableClient;

    public boolean agree(BpmTaskQueryDto bpmTaskQueryDto) {
        log.info("远程调用start======");
        Result result = flowableClient.handle(bpmTaskQueryDto);
        log.info("远程调用end======");
        if (result.getCode() != CommonConstant.FLOWABLE_CODE) {
            log.info("异常错误信息:{},参数:{}", result, bpmTaskQueryDto);
            throw new RemoteException("远程调用提交审批单出现异常代码:" + result.getCode());
        }
        return true;
    }

    public boolean reject(BpmTaskQueryDto bpmTaskQueryDto) {
        log.info("远程调用start======");
        Result result = flowableClient.stopProcessInstance(bpmTaskQueryDto.getTaskId(), bpmTaskQueryDto.getComment());
        log.info("远程调用end======");
        if (result.getCode() != CommonConstant.FLOWABLE_CODE) {
            log.info("异常错误信息:{},参数:{}", result, bpmTaskQueryDto);
            throw new RemoteException("远程调用提交审批单出现异常代码:" + result.getCode());
        }
        return false;
    }

    public boolean rollback(BpmTaskBackQueryDTO bpmTaskBackQueryDTO) {
        log.info("远程调用start======");
        Result<TaskBackVO> result = flowableClient.taskBack(bpmTaskBackQueryDTO);
        log.info("远程调用end======");
        if (result.getCode() != CommonConstant.FLOWABLE_CODE) {
            log.info("异常错误信息:{},参数:{}", result, bpmTaskBackQueryDTO);
            throw new RemoteException("远程调用提交审批单出现异常代码:" + result.getCode());
        }
        return true;
    }

    public Result<ProcessInstanceVO> startProcessInstance(BpmProcessStartQueryDto bpmProcessStartQueryDto) {
        log.info("远程调用start======");
        Result<ProcessInstanceVO> result = flowableClient.startProcessInstance(bpmProcessStartQueryDto);
        log.info("远程调用end======");
        if (result.getCode() != CommonConstant.FLOWABLE_CODE) {
            throw new RemoteException("远程调用发起流程出现异常代码:" + result.getCode());
        }
        return result;
    }

    public Result getAllTodoTasks(BpmTaskCcQueryDto bpmTaskQueryDto) {
        log.info("远程调用start======");
        Result result = flowableClient.getAllTodoTasks(bpmTaskQueryDto);
        log.info("远程调用end======");
        if (result.getCode() != CommonConstant.FLOWABLE_CODE) {
            log.info("异常错误信息:{},参数:{}", result, bpmTaskQueryDto);
            throw new RemoteException("远程调用提交审批单出现异常代码:" + result.getCode());
        }
        return result;
    }

    public Result<List<TaskAssigneeVO>> getTaskAssignee(BpmTaskAssigneeDTO bpmTaskAssigneeDTO) {
        log.info("远程调用start======");
        Result<List<TaskAssigneeVO>> result = flowableClient.getTaskAssignee(bpmTaskAssigneeDTO);
        log.info("远程调用end======");
        if (result.getCode() != CommonConstant.FLOWABLE_CODE) {
            log.info("异常错误信息:{},参数:{}", result, bpmTaskAssigneeDTO);
            throw new RemoteException("远程调用提交审批单出现异常代码:" + result.getCode());
        }
        return result;
    }

    public Result getFinishTasks(BpmTaskFinishQuery finishQuery) {
        log.info("远程调用start======");
        Result result = flowableClient.getFinishTasks(finishQuery);
        log.info("远程调用end======");
        if (result.getCode() != CommonConstant.FLOWABLE_CODE) {
            log.info("异常错误信息:{},参数:{}", result, finishQuery);
            throw new RemoteException("远程调用提交审批单出现异常代码:" + result.getCode());
        }
        return result;
    }

    public Result getAllProcessInstance(BpmTaskFinishQuery finishQuery) {
        log.info("远程调用start======");
        Result result = flowableClient.getAllProcessInstance(finishQuery);
        log.info("远程调用end======");
        if (result.getCode() != CommonConstant.FLOWABLE_CODE) {
            log.info("异常错误信息:{},参数:{}", result, finishQuery);
            throw new RemoteException("远程调用提交审批单出现异常代码:" + result.getCode());
        }
        return result;
    }
}
